package com.google.code.chaotic.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.Embedded;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.Extensions;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

//

@PersistenceCapable
public class CommutingHabit implements Serializable {

	/**
	 * @uml.property name="id"
	 */
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private String parentId;

	/**
	 * @uml.property name="origin"
	 */
	@Persistent
	@Embedded(members = {
			@Persistent(name = "latitude", columns = @javax.jdo.annotations.Column(name = "origLat")),
			@Persistent(name = "longitude", columns = @javax.jdo.annotations.Column(name = "origLon")) })
	private LatLon origin;

	/**
	 * @uml.property name="destination"
	 */
	@Persistent
	@Embedded(members = {
			@Persistent(name = "latitude", columns = @javax.jdo.annotations.Column(name = "destLat")),
			@Persistent(name = "longitude", columns = @javax.jdo.annotations.Column(name = "destLon")) })
	private LatLon destination;

	/**
	 * @uml.property name="departureTime"
	 */
	@Persistent
	private Date departureTime;

	/**
	 * @uml.property name="arrivalTime"
	 */
	@Persistent
	private Date arrivalTime;

	/**
	 * @uml.property name="startDate"
	 */
	@Persistent
	private Date startDate;

	/**
	 * @uml.property name="endDate"
	 */
	@Persistent
	private Date endDate;

	/**
	 * @uml.property name="commuter"
	 * @uml.associationEnd 
	 *                     inverse="commutingHabits:com.google.code.chaotic.model.Commuter"
	 */
	@Persistent
	private Commuter commuter;

	/**
	 * @uml.property name="waypoints"
	 * @uml.associationEnd multiplicity="(0 -1)" ordering="true"
	 *                     aggregation="composite" inverse=
	 *                     "commutingHabit:com.google.code.chaotic.model.HabitWaypoint"
	 */
	@Persistent(mappedBy = "commutingHabit", defaultFetchGroup = "true")
	@Element(dependent = "true")
	private List<HabitWaypoint> waypoints = new ArrayList<HabitWaypoint>();

	/**
	 * @uml.property name="version"
	 */
	@Persistent
	private Date version;

	//

	/** @uml.property name="id" */
	public void setId(String id) {
		this.id = id;
	}

	/** @uml.property name="id" */
	public String getId() {
		return id;
	}

	public String getParentId() {
		return parentId;
	}

	/** @uml.property name="origin" */
	public LatLon getOrigin() {
		return origin;
	}

	/** @uml.property name="origin" */
	public void setOrigin(LatLon origin) {
		this.origin = origin;
	}

	/** @uml.property name="destination" */
	public LatLon getDestination() {
		return destination;
	}

	/** @uml.property name="destination" */
	public void setDestination(LatLon destination) {
		this.destination = destination;
	}

	/** @uml.property name="departureTime" */
	public Date getDepartureTime() {
		return departureTime;
	}

	/** @uml.property name="departureTime" */
	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}

	/** @uml.property name="arrivalTime" */
	public Date getArrivalTime() {
		return arrivalTime;
	}

	/** @uml.property name="arrivalTime" */
	public void setArrivalTime(Date arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	/** @uml.property name="startDate" */
	public Date getStartDate() {
		return startDate;
	}

	/** @uml.property name="startDate" */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/** @uml.property name="endDate" */
	public Date getEndDate() {
		return endDate;
	}

	/** @uml.property name="endDate" */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/** @uml.property name="commuter" */
	public Commuter getCommuter() {
		return commuter;
	}

	/** @uml.property name="commuter" */
	public void setCommuter(Commuter commuter) {
		this.commuter = commuter;
	}

	/** @uml.property name="waypoints" */
	public List<HabitWaypoint> getWaypoints() {
		return waypoints;
	}

	/** @uml.property name="waypoints" */
	public void setWaypoints(List<HabitWaypoint> waypoints) {
		this.waypoints = waypoints;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((origin == null) ? 0 : origin.hashCode());
		return result;
	}

	// @Override
	// public int hashCode() {
	// return id != null ? id.hashCode() : 0;
	// }

	@Override
	public String toString() {
		return String.format("(%1$s, %2$s)", origin, destination);
	}

	/** @uml.property name="version" */
	public Date getVersion() {
		return version;
	}

	/** @uml.property name="version" */
	public void setVersion(Date version) {
		this.version = version;
	}
}
